<?php

class Messages extends HTech_Db_Table
{
    protected $_name    = 'view_messages';
    protected $_primary = 'Id';
    
    protected function _setGridMetadata()
    {
        $this->_gridMetadata['title'] = $this->translate("Messages");
        $this->_gridMetadata['titleMods'] = array('Recipient');
        $this->_gridMetadata['params'] = array('UserId');
        $this->_gridMetadata['onClick'] = 'admin.message.view';
        $this->_gridMetadata['headers'] = array
            (
                array('Date',             180, $this->translate("Date & time"), 'DateTime', 'Date'),
                array('OfficerFullName',  180, $this->translate("Officer"),     'Number'),
                array('LocationName',     180, $this->translate("Branch"),      'Number'),
                array('ProgramName',      180, $this->translate("Program"),     'Number'),
                array('Alert',            600, $this->translate("Message"),     'String')
            );
        return $this;
    }
    
    public function setMessages($data)
    {
        foreach($data as $i => $msg)
            $data[$i]['Alert'] = self::_getMessage($msg);
        return $data;
    }
    
    private function _getMessage($msg)
    {
        switch($msg['Alert']) {
            case 'WRITE_OFF_LOAN': 
                return sprintf($this->translate("$%s written-off of %s's code %s loan"),
                number_format($msg['Amount'],2,'.',','), $msg['ClientFullName'],$msg['LoanId']); 
            case 'WRITE_OFF_PAYMENT': 
                return sprintf($this->translate("Payment applied to %s's code %s written-off loan"),
                    $msg['ClientFullName'],$msg['LoanId']); 
            case 'WRITE_OFF_CHARGE':
                return sprintf($this->translate("Legal charge applied to %s's code %s written-off loan"),
                    $msg['ClientFullName'],$msg['LoanId']);
            case 'WRITE_OFF_RECOVERED':
                return sprintf($this->translate("%s's code %s written-off loan has been fully recovered"),
                    $msg['ClientFullName'],$msg['LoanId']);
            case 'WRITE_OFF_NEW_LOAN_REQUEST':
                return sprintf($this->translate("New loan requested for %s, who had loans previously written-off"),
                    $msg['ClientFullName']);
            case 'WRITE_OFF_RISK': 
                return sprintf($this->translate("%s's loan code %s will be written-off if no payment is applied by %s"),
                    $msg['ClientFullName'],$msg['LoanId'],date('Y-m-d', $msg['Deadline'])); 
            case 'WRITE_OFF_LARGE': 
                return sprintf($this->translate("$%s written-off of %s's code %s loan"),
                    number_format($msg['Amount'],2,'.',','), $msg['ClientFullName'],$msg['LoanId']); 
            }
    }
    
    
}